package com.example.bilibili.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.bilibili.domain.BiliVideo;
import com.example.bilibili.domain.Bullet;
import com.example.bilibili.domain.Comment;
import com.example.bilibili.service.BiliVideoService;
import com.example.bilibili.service.BulletService;
import com.example.bilibili.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class VideoController {
    @Autowired
    private BiliVideoService biliVideoService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private BulletService bulletService;
    @RequestMapping("/findAllVideos")
    public Object findAllVideos(){
        QueryWrapper qw= new QueryWrapper();
        qw.select("id","videoId","videoTitle","videoIntrbriefing","thumbs","collection","coin","forwarding","bullet","comment","uploadtime","uploaderId","videoPath","auditingID","grounding","coverImage","memo");
        return biliVideoService.list(qw);
    }

    @RequestMapping("/findVideoByAV")
    public Object findAllVideos(String videoId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("id","videoId","videoTitle","videoIntrbriefing","thumbs","collection","coin","forwarding","bullet","comment","uploadtime","uploaderId","videoPath","auditingID","grounding","coverImage","memo");
        qw.like("videoId",videoId);
        return biliVideoService.list(qw);
    }

    //遍历审核状态
    @RequestMapping("/findAllVideoAuditState")
    public Object findAllVideoAuditState(){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","videoPath");
        qw.eq("auditingID",5);
        return biliVideoService.list(qw);
    }

    //通过AV号查询审核状态
    @RequestMapping("/findVideoAuditStateByAV")
    public Object findVideoAuditStateByAV(String videoId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId");
        qw.eq("videoId",videoId);
        qw.eq("auditingID",5);
        return biliVideoService.list(qw);
    }

    //通过上传人ID查询审核状态
    @RequestMapping("/findVideoAuditStateByUpId")
    public Object findVideoAuditStateByUpId(String uploaderId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId");
        qw.eq("uploaderId",uploaderId);
        qw.eq("auditingID",5);
        return biliVideoService.list(qw);
    }

    //通过AV号和上传人ID查询审核状态
    @RequestMapping("/findVideoAuditStateByAVAndUpId")
    public Object findVideoAuditStateByAV(String videoId,String uploaderId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId");
        qw.eq("videoId",videoId);
        qw.eq("uploaderId",uploaderId);
        qw.eq("auditingID",5);
        return biliVideoService.list(qw);
    }

    //遍历待上架状态
    @RequestMapping("/findVideoUploadState")
    public Object findVideoUploadOrRemoveState(){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("auditingID",6);
        return biliVideoService.list(qw);
    }

    //通过AV号查询上架状态
    @RequestMapping("/findVideoUploadStateByAV")
    public Object findVideoUploadStateByAV(String videoId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("videoId",videoId);
        qw.eq("auditingID",6);
        return biliVideoService.list(qw);
    }

    //通过上传人ID查询上架状态
    @RequestMapping("/findVideoUploadStateByUpId")
    public Object findVideoUploadStateByUpId(String uploaderId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("uploaderId",uploaderId);
        qw.eq("auditingID",6);
        return biliVideoService.list(qw);
    }

    //通过AV号和上传人ID查询上架状态
    @RequestMapping("/findVideoUploadStateByAVAndUpId")
    public Object findVideoUploadStateByAVAndUpId(String videoId,String uploaderId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("videoId",videoId);
        qw.eq("uploaderId",uploaderId);
        qw.eq("auditingID",6);
        return biliVideoService.list(qw);
    }

    //遍历下架状态
    @RequestMapping("/findVideoRemoveState")
    public Object findVideoRemoveState(){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("auditingID",7);
        return biliVideoService.list(qw);
    }

    //通过AV号查询下架状态
    @RequestMapping("/findVideoRemoveStateByAV")
    public Object findVideoRemoveStateByAV(String videoId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("videoId",videoId);
        qw.eq("auditingID",7);
        return biliVideoService.list(qw);
    }

    //通过上传人ID查询下架状态
    @RequestMapping("/findVideoRemoveStateByUpId")
    public Object findVideoRemoveStateByUpId(String uploaderId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("uploaderId",uploaderId);
        qw.eq("auditingID",7);
        return biliVideoService.list(qw);
    }

    //通过AV号和上传人ID查询下架状态
    @RequestMapping("/findVideoRemoveStateByAVAndUpId")
    public Object findVideoRemoveStateByAVAndUpId(String videoId,String uploaderId){
        QueryWrapper qw= new QueryWrapper();
        qw.select("videoId","videoTitle","videoIntrbriefing","uploadtime","uploaderId","auditingID");
        qw.eq("videoId",videoId);
        qw.eq("uploaderId",uploaderId);
        qw.eq("auditingID",7);
        return biliVideoService.list(qw);
    }

    //通过接收AV号从上架改为下架状态
    @RequestMapping("/modifyU2RByAV")
    public int modifyU2RByAV(String videoId){
        UpdateWrapper uw=new UpdateWrapper();
        uw.eq("videoId",videoId);
        BiliVideo bv=biliVideoService.getOne(uw);
        bv.setAuditingid(6);
        return biliVideoService.update(bv,uw)?1:0;
    }

    //通过接收AV号从下架改为上架状态
    @RequestMapping("/modifyR2UByAV")
    public Object modifyR2UByAV(String videoId){
        UpdateWrapper uw=new UpdateWrapper();
        uw.eq("videoId",videoId);
        BiliVideo bv=biliVideoService.getOne(uw);
        bv.setAuditingid(7);
        return biliVideoService.update(bv,uw)?1:0;
    }

    //通过videoId级联删除video/bullet/comment
    @RequestMapping("/deleteVideoByAV")
    public int deleteVideoByAV(String videoId){
        QueryWrapper<Comment> qw1= new QueryWrapper<>();
        qw1.select("id").eq("videoId",videoId);
        List<Comment> co=commentService.list(qw1);
        if(co!=null)
            commentService.removeByIds(co);

        QueryWrapper qw2= new QueryWrapper();
        qw2.select("id").eq("videoId",videoId);
        List<Bullet> bu=bulletService.list(qw2);
        if(bu!=null)
            bulletService.removeByIds(bu);

        QueryWrapper qw3= new QueryWrapper();
        qw3.select("id").eq("videoId",videoId);
        List<BiliVideo> bv=biliVideoService.list(qw3);
        if(bv!=null)
            biliVideoService.removeByIds(bv);
        return 1;
    }

    //通过videoId将auditingID改为6
    @RequestMapping("/modifyAuditingIDt6ByAV")
    public Object modifyAuditingIDt6ByAV(String videoId){
        UpdateWrapper uw=new UpdateWrapper();
        uw.eq("videoId",videoId);
        BiliVideo bv=biliVideoService.getOne(uw);
        bv.setAuditingid(6);
        return biliVideoService.update(bv,uw)?1:0;
    }
    //通过videoId将auditingID改为7
    @RequestMapping("/modifyAuditingIDt7ByAV")
    public Object modifyAuditingIDt7ByAV(String videoId){
        UpdateWrapper uw=new UpdateWrapper();
        uw.eq("videoId",videoId);
        BiliVideo bv=biliVideoService.getOne(uw);
        bv.setAuditingid(7);
        return biliVideoService.update(bv,uw)?1:0;
    }
}
